package com.ruoyi.Course.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.Goover.domain.StudyEnglish;
import com.ruoyi.Goover.mapper.StudyEnglishMapper;
import com.ruoyi.Label.domain.StudyLable;
import com.ruoyi.Label.mapper.StudyLableMapper;
import com.ruoyi.Unit.domain.StudyUnit;
import com.ruoyi.Unit.mapper.StudyUnitMapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Course.domain.StudyCourse;
import com.ruoyi.Course.service.IStudyCourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * CourseController
 * 
 * @author luobom
 * @date 2021-12-03
 */
@RestController
@RequestMapping("/Course/Course")
public class StudyCourseController extends BaseController
{
    @Autowired
    private IStudyCourseService studyCourseService;

    @Autowired
    private StudyUnitMapper studyUnitMapper;

    @Autowired
    private StudyLableMapper studyLableMapper;

    @Autowired
    private StudyEnglishMapper studyEnglishMapper;
    /**
     * 设置默认课程
     */
    @GetMapping(value = "/SetDefaultCourse/{uid}")
    public Boolean SetDefaultCourse(@PathVariable("uid") String uid)
    {
        //取消已经设置的默认课程
        studyCourseService.updatedefaultcourse();
        //设置默认课程
        Map m=new HashMap();
        m.put("createBy",String.valueOf(SecurityUtils.getUserId()));
        m.put("uid",uid);
        return studyCourseService.updatedefaultcourseset(m)>0?true:false;
    }

    /**
     * 获取默认课程
     */
    @PreAuthorize("@ss.hasPermi('Course:Course:list')")
    @GetMapping(value = "/GetDefaultCourse")
    public StudyCourse GetDefaultCourse()
    {
        return studyCourseService.GetDefaultCourse();
    }

    /**
     * 查询Course列表
     */
    @PreAuthorize("@ss.hasPermi('Course:Course:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudyCourse studyCourse)
    {
        startPage();
        List<StudyCourse> list = studyCourseService.selectStudyCourseList(studyCourse);
        return getDataTable(list);
    }

    /**
     * 导出Course列表
     */
    @PreAuthorize("@ss.hasPermi('Course:Course:export')")
    @Log(title = "Course", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudyCourse studyCourse)
    {
        List<StudyCourse> list = studyCourseService.selectStudyCourseList(studyCourse);
        ExcelUtil<StudyCourse> util = new ExcelUtil<StudyCourse>(StudyCourse.class);
        util.exportExcel(response, list, "Course数据");
    }

    /**
     * 获取Course详细信息
     */
    @PreAuthorize("@ss.hasPermi('Course:Course:query')")
    @GetMapping(value = "/{uid}")
    public AjaxResult getInfo(@PathVariable("uid") String uid)
    {
        return AjaxResult.success(studyCourseService.selectStudyCourseByUid(uid));
    }


    /**
     * 新增Course
     */
    @PreAuthorize("@ss.hasPermi('Course:Course:add')")
    @Log(title = "Course", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudyCourse studyCourse)
    {
        StudyCourse sc=studyCourseService.GetDefaultCourse();
        if(sc.getUid()==null){
            studyCourse.setDefaultcourse("1");
        }
        return toAjax(studyCourseService.insertStudyCourse(studyCourse));
    }

    /**
     * 修改Course
     */
    @PreAuthorize("@ss.hasPermi('Course:Course:edit')")
    @Log(title = "Course", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudyCourse studyCourse)
    {
        return toAjax(studyCourseService.updateStudyCourse(studyCourse));
    }

    /**
     * 删除Course
     */
    @PreAuthorize("@ss.hasPermi('Course:Course:remove')")
    @Log(title = "Course", businessType = BusinessType.DELETE)
	@DeleteMapping("/{uids}")
    public AjaxResult remove(@PathVariable String[] uids)
    {
        StudyUnit su;
        StudyLable sl;
        StudyEnglish sd;
        for(String uid:uids){
            su=new StudyUnit();
            sl=new StudyLable();
            sd=new StudyEnglish();

            su.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            sl.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            sd.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            su.setCourseuid(uid);
            //卡片
            if(studyUnitMapper.selectStudyUnitList(su).size()>0){
                return toAjax(0);
            }

            //标签
            sl.setCourseuid(uid);
            if(studyLableMapper.selectStudyLableList(sl).size()>0){
                return toAjax(0);
            }

            //单词
            sd.setCourseuid(uid);
            if(studyEnglishMapper.selectStudyEnglishList(sd).size()>0){
                return toAjax(0);
            }


        }


        return toAjax(studyCourseService.deleteStudyCourseByUids(uids));
    }


}
